﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoyoEden.Models;
using System.Web;
using System.Globalization;
using System.Web.Security;
using System.IO;
using System.Web.UI;

namespace CoyoEden.Web.Controls
{
	public class TopicViewBase:ViewBase
	{
		#region Properties
		public Topic TheTopic { get; set; }
		public List<Comment> CommentsShow { get; set; }
		public bool ShowAdminLink { get; set; }
		public bool TailContent { get; set; }
		#endregion

		#region Readonly Member variables
		/// <summary>
		/// Displays a delete link to visitors that are authenticated
		/// using the default membership provider.
		/// </summary>
		protected string AdminLinks
		{
			get
			{
				if (Page.User.IsInRole(PortalSettings.Instance.AdministratorRole))
				{
					System.Text.StringBuilder sb = new System.Text.StringBuilder();
																																											 
					if (!TheTopic.IsApproved)
					{
						sb.AppendFormat("<a href=\"#;\" onclick=\"TopicList.Approve({0})\" title=\"{1}\">{1}</a>", "{id:'"+TheTopic.Id+"',obj:this}", "审核");

					}
					//sb.AppendFormat("<a href=\"#;\" onclick=\"TopicList.SetTags({id:'{0}'})\" title=\"{1}\">{1}</a>", TheTopic.Id, "设置标签");
					sb.AppendFormat(" | <a href=\"#;\" onclick=\"TopicList.Del({0})\" title=\"{1}\">{1}</a>", "{id:'" + TheTopic.Id + "'}", "删除");

					return sb.ToString();
				}
				return string.Empty;
			}
		}

		private const string GRAVATAR_IMAGE = "<img class=\"photo\" src=\"{0}\" alt=\"{1}\" />";

		
		#endregion

		#region Methods
		/// <summary>
		/// Displays the Gravatar image that matches the specified email.
		/// </summary>
		protected string Gravatar(int size)
		{
			if (PortalSettings.Instance.Avatar == "none")
				return null;

			if (String.IsNullOrEmpty(TheTopic.AuthorProfile.Email) || !TheTopic.AuthorProfile.Email.Contains("@"))
			{
				if (TheTopic.AuthorProfile.Website != null && TheTopic.AuthorProfile.Website.ToString().Length > 0 && TheTopic.AuthorProfile.Website.ToString().Contains("http://"))
				{
					return string.Format(CultureInfo.InvariantCulture, "<img class=\"thumb\" src=\"http://images.websnapr.com/?url={0}&amp;size=t\" alt=\"{1}\" />", Server.UrlEncode(TheTopic.AuthorProfile.Website.ToString()), TheTopic.AuthorProfile.Email);
				}

				return "<img src=\"" + Utils.AbsoluteWebRoot + "themes/" + PortalSettings.Instance.Theme + "/img/noavatar.jpg\" alt=\"" + TheTopic.AuthorProfile.Name + "\" />";
			}

			string hash = FormsAuthentication.HashPasswordForStoringInConfigFile(TheTopic.AuthorProfile.Email.ToLowerInvariant().Trim(), "MD5").ToLowerInvariant();
			string gravatar = "http://www.gravatar.com/avatar/" + hash + ".jpg?s=" + size + "&amp;d=";

			string link = string.Empty;
			switch (PortalSettings.Instance.Avatar)
			{
				case "identicon":
					link = gravatar + "identicon";
					break;

				case "wavatar":
					link = gravatar + "wavatar";
					break;

				default:
					link = gravatar + "monsterid";
					break;
			}

			return string.Format(CultureInfo.InvariantCulture, GRAVATAR_IMAGE, link, TheTopic.AuthorProfile.Name);
		}
		#endregion
	}
}
